use master

declare @databases cursor 
declare @dbname sysname, @sql varchar(8000), @user sysname

set @user = 'guiga'

set @databases = cursor for select name from sysdatabases where name in ( 'freire_sistemas', 'trb_lauro_delphi')

open @databases 
fetch next from @databases into @dbname
set @sql = ''
while @@fetch_status = 0
begin
	-- Aqui algum comando

	set @sql = 'use ' + @dbname + char(13) + char(10) 
	set @sql = @sql  + 'declare @tmp table (sqlstring sysname) '  + char(13) + char(10) 
	set @sql = @sql  + 'insert into @tmp (sqlstring) '    + char(13) + char(10)
	set @sql = @sql  + 'select ''GRANT SELECT ON ' + ''' + ' + 'name + '' TO ' 
			 + @user + ''' from ' + @dbname + '..sysobjects where xtype = ' + char(39) + 'u' + char(39)
	set @sql = @sql  + ' declare @t sysname,@c cursor ' + char(13) + char(10)
	set @sql = @sql  + 'set @c=cursor for select sqlstring from @tmp ' + char(13) + char(10)
	set @sql = @sql  + 'open @c ' + char(13) + char(10)
	set @sql = @sql  + 'fetch next from @c into @t ' + char(13) + char(10)
	set @sql = @sql  + 'while @@fetch_status = 0 begin ' + char(13) + char(10)
	set @sql = @sql  + 'exec(@t)' + char(13) + char(10)
	set @sql = @sql  + 'fetch next from @c into @t '+ char(13) + char(10)
	set @sql = @sql  + 'end close @c deallocate @c' + char(13) + char(10) + char(10)
	exec(@sql)
	-- print @sql
	fetch next from @databases into @dbname
end

close @databases 
deallocate @databases 

-- print @sql
-- exec(@sql) 